package com.ww.springboot.boot.algorithm.leetcode1;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-28 14:38
 */
public class BW905按奇偶排序数组 {

    public static void main(String[] args) {

        int[] nums = {0,1};

        int[] numsNew = sortArrayByParity(nums);

        PrintUtil.print(numsNew);
    }

    /**
     * 双指针 左指针记录奇数 右指针记录偶数 同时满足时交换
     * @param nums
     * @return
     */
    public static int[] sortArrayByParity(int[] nums) {

        int left = 0;
        int right = nums.length -1;

        while (right>left){
            if(nums[left]%2 != 0 &&nums[right]%2 == 0){
                int num = nums[left];
                nums[left] = nums[right];
                nums[right] = num;
                left++;
                right--;
            }
            if(nums[left]%2 == 0){
                left++;
            }
            if(nums[right]%2 != 0){
                right--;
            }
        }
        return nums;
    }



}
